﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
using Benefit.Models.Week;
namespace Benefit.Service.Week
{
    /// <summary>
    /// 同批次交易员均值
    /// </summary>
    public class ServiceBatchAvgWeek : Benefit.Interface.Week.IBatchAvgWeek
    {
        Benefit.DB.DBManager db = null;
        public ServiceBatchAvgWeek(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当周所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int week, int month, int year)
        {
            var query = from t in db.BatchAvgWeek where t.Week == week && t.Year == year select t;
            foreach (Models.Week.BatchAvgWeek c in query)
            {
                db.BatchAvgWeek.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存周同批次交易员均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitBatchWeekAvg(int week, int month, int year)
        {
            List<Models.Sys.Batch> batch = db.Batches.ToList();
            //Interface.Sys.IBatch ib = new Sys.ServiceBatch(db);
            foreach (Models.Sys.Batch b in batch)
            {
                BatchAvgWeek w = new BatchAvgWeek();
                w.BatchId = b.Id;

                var queryExist = from t in db.BatchAvgWeek where t.BatchId == b.Id && t.Year == year && t.Week == week select t;
                if (queryExist.Count() > 0)
                {
                    var query = from t in db.BatchAvgDay where t.TradeHistory.WPdate == week && t.TradeHistory.YPdate == year && t.BatchId == b.Id select t;
                    if (query.Count() > 0)
                    {
                        queryExist.First().BillCount = query.Sum(a => a.BillCount);
                        queryExist.First().DayCount = query.Sum(a => a.DayCount);
                        queryExist.First().Free = query.Sum(a => a.Free);
                        queryExist.First().LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                        queryExist.First().Profit = query.Sum(a => a.Profit);
                        queryExist.First().SumLost = query.Sum(a => a.SumLost);
                        queryExist.First().SumWin = query.Sum(a => a.SumWin);
                        queryExist.First().UsedMargin = query.Sum(a => a.UsedMargin);
                        queryExist.First().WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                        queryExist.First().AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                        queryExist.First().AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                        queryExist.First().MaxReturn = query.Sum(a => a.MaxReturn);
                        queryExist.First().OverStopLoss = query.Sum(a => a.OverStopLoss);
                        queryExist.First().PingJinPing = Convert.ToInt32(query.Sum(a => a.PingJinPing));
                        queryExist.First().RaiseRate = query.Sum(a => a.RaiseRate);
                        queryExist.First().RiskCount = Convert.ToInt32(query.Sum(a => a.RiskCount));
                        queryExist.First().Utilization = query.Sum(a => a.Utilization);
                        queryExist.First().WinRate = query.Sum(a => a.WinRate);
                        queryExist.First().Yield = query.Sum(a => a.Yield);
                    }
                }
                else
                {
                    w.Month = month;
                    w.Week = week;
                    w.Year = year;

                    var query = from t in db.BatchAvgDay where t.TradeHistory.WPdate == week && t.TradeHistory.YPdate == year && t.BatchId == b.Id select t;
                    if (query.Count() > 0)
                    {
                        w.BillCount = query.Sum(a => a.BillCount);
                        w.DayCount = query.Sum(a => a.DayCount);
                        w.Free = query.Sum(a => a.Free);
                        w.LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                        w.Profit = query.Sum(a => a.Profit);
                        w.SumLost = query.Sum(a => a.SumLost);
                        w.SumWin = query.Sum(a => a.SumWin);
                        w.UsedMargin = query.Sum(a => a.UsedMargin);
                        w.WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                        w.AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                        w.AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                        w.MaxReturn = query.Sum(a => a.MaxReturn);
                        w.OverStopLoss = query.Sum(a => a.OverStopLoss);
                        w.PingJinPing = Convert.ToInt32(query.Sum(a => a.PingJinPing));
                        w.RaiseRate = query.Sum(a => a.RaiseRate);
                        w.RiskCount = Convert.ToInt32(query.Sum(a => a.RiskCount));
                        w.Utilization = query.Sum(a => a.Utilization);
                        w.WinRate = query.Sum(a => a.WinRate);
                        w.Yield = query.Sum(a => a.Yield);
                        db.BatchAvgWeek.Add(w);
                    }

                }


            }
            db.SaveChanges();
        }
    }
}